<template>
	<view v-show="show">
		<!-- -->
		<u-picker :show="show" :columns="columns" keyName="label" @confirm="confirm" @cancel="close"></u-picker>
		<permissionModel ref="permissionModel" :permissionContent="permissionContent" />
		<model ref="model" :content="content" :openAppPermission="true" @toOpenAppPermission="toOpenAppPermission" />
	</view>
</template>
<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	import {
		openAppSettingPermissions,
		requestAndroidPermission
	} from '@/common/index.js'
	export default {
		name: 'DongxianUploadImg',
		props: {
			value: {
				type: [Array, String],
				default: ''
			},
			permissionCameraContent: {
				type: String,
				default: '获取拍照的权限，是为了能够上传头像'
			},
			permissionAlbumContent: {
				type: String,
				default: '获取相册的权限，是为了能够上传头像'
			},
			singleNum: {
				type: Boolean,
				default: false
			}
		},
		computed: {
			imageUrl: {
				get() {
					return this.value || []
				},
				set(val) {
					this.$emit('input', val)
				}
			}
		},
		data() {
			return {
				permissionContent: '',
				content: '',
				content1: '上传图片需要打开相机的权限，请到应用设置里面开启。',
				content2: '上传图片需要打开相册的权限，请到应用设置里面开启。',
				show: false,
				columns: [
					[{
						label: '相机',
						id: 1
					}, {
						label: '相册',
						id: 2
					}]
				],
			};
		},

		mounted() {

		},

		methods: {
			toOpenAppPermission() {
				this.$refs.permissionModel.close()
			},
			async confirm(e) {
				console.log('%c [ e ]-33', 'font-size:13px; background:pink; color:#bf2c9f;', e)
				let selectId = e.value[0].id
				console.log('%c [ selectId ]-35', 'font-size:13px; background:pink; color:#bf2c9f;', selectId)
				let columns = this.columns[0]
				// #ifdef APP-PLUS
				let {
					platform
				} = uni.getSystemInfoSync('systemInfo')
				if (+selectId === 1) {
					if (platform == 'android') {
						this.androidCameraPermission('camera', this.content1, this.permissionCameraContent)
					} else if (platform == 'ios') {
						this.uploadCamera()
					}
				} else if (+selectId === 2) { // 打开相册
					if (platform == 'android') {
						console.log('%c [ platform ]-80', 'font-size:13px; background:pink; color:#bf2c9f;', platform)
						// this.content = this.content2
						// this.$refs.model.open()
						this.androidAlbumPermission('album', this.content2, this.permissionAlbumContent)
					} else if (platform == 'ios') {
						this.uploadAlbum()
					}
				}
				// #endif
				// #ifdef H5
				if (+selectId === 1) {
					return
				} else if (+selectId === 2) { // 打开相册
					this.uploadAlbum(); // 同意则调用方法
				}
				// #endif

				// this.show = false;/
			},
			openAppPermissionAndUploadCameraImage() {
        // #ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform == 'android') {
					this.androidCameraPermission('camera', this.content1, this.permissionCameraContent)
				} else if (uni.getSystemInfoSync().platform == 'ios') {
					this.uploadCamera()
				}
        // #endif
        // #ifdef H5
				this.uploadCamera(); // 同意则调用方法
				// #endif
			},
			openAppPermissionAndUploadAlbumImage() {
        // #ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform == 'android') {
					this.androidAlbumPermission('album', this.content2, this.permissionAlbumContent)
				} else if (uni.getSystemInfoSync().platform == 'ios') {
					this.uploadAlbum()
				}
        // #endif
        // #ifdef H5
				this.uploadAlbum(); // 同意则调用方法
				// #endif
			},
      openAppPermissionAndUploadCameraVideo() {
        // #ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform == 'android') {
					this.androidCameraVideoPermission('camera', this.content1, this.permissionCameraContent)
				} else if (uni.getSystemInfoSync().platform == 'ios') {
					this.uploadCamera()
				}
        // #endif
        // #ifdef H5
				this.uploadCamera(); // 同意则调用方法
				// #endif
			},
      openAppPermissionAndUploadAlbumVideo() {
				// #ifdef APP-PLUS
        if (uni.getSystemInfoSync().platform == 'android') {
					this.androidAlbumVideoPermission('album', this.content2, this.permissionAlbumContent)
				} else if (uni.getSystemInfoSync().platform == 'ios') {
					this.sendVideoMessage()
				}
        // #endif
        // #ifdef H5
				this.uploadAlbum(); // 同意则调用方法
				// #endif
        // sendVideoMessage
      },
			open() {
				this.show = true;
				console.log('%c [ this.show ]-121', 'font-size:13px; background:pink; color:#bf2c9f;', this.show)
			},
			close() {
				this.show = false;
			},
			androidCameraPermission(type, content, permissionContent) {
				console.log('%c [ type ]-101', 'font-size:13px; background:pink; color:#bf2c9f;', type)
				let options = {
					'camera': 'android.permission.CAMERA',
					'album': 'android.permission.WRITE_EXTERNAL_STORAGE'
				}
				let permission = options[type]
				// let permission = type === 'camera' ? 'CAMERA' : 'READ_EXTERNAL_STORAGE'
				console.log('%c [ permission ]-93', 'font-size:13px; background:pink; color:#bf2c9f;', permission)
				var Manifest = plus.android.importClass("android.Manifest");
				var MainActivity = plus.android.runtimeMainActivity();
				let result = MainActivity.checkSelfPermission(Manifest.permission[permission]);
				console.log('%c [ result ]-63', 'font-size:13px; background:pink; color:#bf2c9f;', result)
				if (result) {
					plus.android.requestPermissions([permission], e => {
						console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
						if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
							this.content = content
							this.$refs.model.open()
							this.$refs.permissionModel.close()
						} else if (e.deniedPresent.length > 0) {
							this.$refs.permissionModel.close()
						} else if (e.granted.length > 0) {
							this.$refs.permissionModel.close(); // 别忘了关闭提示
							if (type == 'camera') {
								this.uploadCamera(); // 同意则调用方法
							} else if (type == 'album') {
								this.uploadAlbum(); // 同意则调用方法
							}
						}
					})
					// this.uploadCamera(); // 有权限则直接调用方法
				} else {
					// this.uploadCamera()
					this.permissionContent = permissionContent
					this.$refs.permissionModel.open()
					// this.permissionContent = ''
					plus.android.requestPermissions([permission], e => {
						console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
						if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
							this.content = content
							this.$refs.permissionModel.close()
							this.$refs.model.open()
						} else if (e.deniedPresent.length > 0) {
							this.$refs.permissionModel.close()
						} else if (e.granted.length > 0) {
							this.$refs.permissionModel.close(); // 别忘了关闭提示
							if (type == 'camera') {
								this.uploadCamera(); // 同意则调用方法
							} else if (type == 'album') {
								this.uploadAlbum(); // 同意则调用方法
							}
						}
					})
				}
			},
			androidAlbumPermission(type, content, permissionContent) {
        console.log('%c [ permissionContent ]-186', 'font-size:13px; background:pink; color:#bf2c9f;', permissionContent)
        console.log('%c [ content ]-186', 'font-size:13px; background:pink; color:#bf2c9f;', content)
				console.log('%c [ type ]-101', 'font-size:13px; background:pink; color:#bf2c9f;', type)
				let options = {
					'camera': 'android.permission.CAMERA',
					'album': 'android.permission.WRITE_EXTERNAL_STORAGE'
				}
				let permission = options[type]
				// let permission = type === 'camera' ? 'CAMERA' : 'READ_EXTERNAL_STORAGE'
				console.log('%c [ permission ]-93', 'font-size:13px; background:pink; color:#bf2c9f;', permission)
				var Manifest = plus.android.importClass("android.Manifest");
				var MainActivity = plus.android.runtimeMainActivity();
				let result = MainActivity.checkSelfPermission(Manifest.permission[permission]);
				console.log('%c [ result ]-63', 'font-size:13px; background:pink; color:#bf2c9f;', result)
				if (result) {
					plus.android.requestPermissions([permission], e => {
						console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
						if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
							this.content = content
							this.$refs.model.open()
							this.$refs.permissionModel.close()
						} else if (e.deniedPresent.length > 0) {
							this.$refs.permissionModel.close()
						} else if (e.granted.length > 0) {
							this.$refs.permissionModel.close(); // 别忘了关闭提示
							if (type == 'camera') {
								this.uploadCamera(); // 同意则调用方法
							} else if (type == 'album') {
								this.uploadAlbum(); // 同意则调用方法
							}
						}
					})
					// this.uploadCamera(); // 有权限则直接调用方法
				} else {
					// this.uploadCamera()
					this.permissionContent = permissionContent
          console.log('%c [ permissionContent ]-222', 'font-size:13px; background:pink; color:#bf2c9f;', permissionContent)
					this.$refs.permissionModel.open()
					// this.permissionContent = ''
					plus.android.requestPermissions([permission], e => {
						console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
						if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
							this.content = content
							this.$refs.permissionModel.close()
							this.$refs.model.open()
						} else if (e.deniedPresent.length > 0) {
							this.$refs.permissionModel.close()
						} else if (e.granted.length > 0) {
							this.$refs.permissionModel.close(); // 别忘了关闭提示
							if (type == 'camera') {
								this.uploadCamera(); // 同意则调用方法
							} else if (type == 'album') {
								this.uploadAlbum(); // 同意则调用方法
							}
						}
					})
				}
			},
      androidCameraVideoPermission(type, content, permissionContent) {
				console.log('%c [ type ]-101', 'font-size:13px; background:pink; color:#bf2c9f;', type)
				let options = {
					'camera': 'android.permission.CAMERA',
					'album': 'android.permission.WRITE_EXTERNAL_STORAGE'
				}
				let permission = options[type]
				// let permission = type === 'camera' ? 'CAMERA' : 'READ_EXTERNAL_STORAGE'
				console.log('%c [ permission ]-93', 'font-size:13px; background:pink; color:#bf2c9f;', permission)
				var Manifest = plus.android.importClass("android.Manifest");
				var MainActivity = plus.android.runtimeMainActivity();
				let result = MainActivity.checkSelfPermission(Manifest.permission[permission]);
				console.log('%c [ result ]-63', 'font-size:13px; background:pink; color:#bf2c9f;', result)
				if (result) {
					plus.android.requestPermissions([permission], e => {
						console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
						if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
							this.content = content
							this.$refs.model.open()
							this.$refs.permissionModel.close()
						} else if (e.deniedPresent.length > 0) {
							this.$refs.permissionModel.close()
						} else if (e.granted.length > 0) {
							this.$refs.permissionModel.close(); // 别忘了关闭提示
              this.sendVideoMessage(type)
						}
					})
					// this.uploadCamera(); // 有权限则直接调用方法
				} else {
					// this.uploadCamera()
					this.permissionContent = permissionContent
					this.$refs.permissionModel.open()
					// this.permissionContent = ''
					plus.android.requestPermissions([permission], e => {
						console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
						if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
							this.content = content
							this.$refs.permissionModel.close()
							this.$refs.model.open()
						} else if (e.deniedPresent.length > 0) {
							this.$refs.permissionModel.close()
						} else if (e.granted.length > 0) {
							this.$refs.permissionModel.close(); // 别忘了关闭提示
              this.sendVideoMessage(type)
						}
					})
				}
			},
			androidAlbumVideoPermission(type, content, permissionContent) {
        console.log('%c [ permissionContent ]-186', 'font-size:13px; background:pink; color:#bf2c9f;', permissionContent)
        console.log('%c [ content ]-186', 'font-size:13px; background:pink; color:#bf2c9f;', content)
				console.log('%c [ type ]-101', 'font-size:13px; background:pink; color:#bf2c9f;', type)
				let options = {
					'camera': 'android.permission.CAMERA',
					'album': 'android.permission.WRITE_EXTERNAL_STORAGE'
				}
				let permission = options[type]
				// let permission = type === 'camera' ? 'CAMERA' : 'READ_EXTERNAL_STORAGE'
				console.log('%c [ permission ]-93', 'font-size:13px; background:pink; color:#bf2c9f;', permission)
				var Manifest = plus.android.importClass("android.Manifest");
				var MainActivity = plus.android.runtimeMainActivity();
				let result = MainActivity.checkSelfPermission(Manifest.permission[permission]);
				console.log('%c [ result ]-63', 'font-size:13px; background:pink; color:#bf2c9f;', result)
				if (result) {
					plus.android.requestPermissions([permission], e => {
						console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
						if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
							this.content = content
							this.$refs.model.open()
							this.$refs.permissionModel.close()
						} else if (e.deniedPresent.length > 0) {
							this.$refs.permissionModel.close()
						} else if (e.granted.length > 0) {
							this.$refs.permissionModel.close(); // 别忘了关闭提示
              this.sendVideoMessage(type)
						}
					})
					// this.uploadCamera(); // 有权限则直接调用方法
				} else {
					// this.uploadCamera()
					this.permissionContent = permissionContent
          console.log('%c [ permissionContent ]-222', 'font-size:13px; background:pink; color:#bf2c9f;', permissionContent)
					this.$refs.permissionModel.open()
					// this.permissionContent = ''
					plus.android.requestPermissions([permission], e => {
						console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
						if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
							this.content = content
							this.$refs.permissionModel.close()
							this.$refs.model.open()
						} else if (e.deniedPresent.length > 0) {
							this.$refs.permissionModel.close()
						} else if (e.granted.length > 0) {
							this.$refs.permissionModel.close(); // 别忘了关闭提示
              this.sendVideoMessage(type)
						}
					})
				}
			},
			uploadCamera() {
        console.log('%c [ uploadCamera ]-255', 'font-size:13px; background:pink; color:#bf2c9f;', 'uploadCamera')
				const that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera'], //从相册选择
					// sourceType: ['camera', 'album'], //从相册选择
					success: function(res) {
						let tempFilePaths = res.tempFilePaths
						// console.log(formData)
						let url = that.BASE_URL + 'addons/cos/index/uploads'
						console.log('%c [ url ]-663', 'font-size:13px; background:pink; color:#bf2c9f;', url)
						tempFilePaths.forEach(function(item) {
							uni.showLoading({
								title: '上传中...'
							})
							uni.uploadFile({
								url: url, //仅为示例，非真实的接口地址
								filePath: item,
								name: 'file',
								header: {
									token: uni.getStorageSync('token')
								},
								success: (a) => {
									uni.hideLoading()
									console.log(a)
									console.log(JSON.parse(a.data))
									// that.uploadImgUrl = JSON.parse(a.data).data.fullurl
									// that.$emit('input', JSON.parse(a.data).data.fullurl)
									if (that.singleNum) {
                    console.log('%c [ JSON.parse(a.data).data.fullurl ]-331', 'font-size:13px; background:pink; color:#bf2c9f;', JSON.parse(a.data).data.fullurl)
                    that.imageUrl = ''
										that.imageUrl = JSON.parse(a.data).data.fullurl
                    let imageUrl = JSON.parse(a.data).data.fullurl
									  that.$emit('uploadImageSuccess', imageUrl,res)
                    console.log('%c [ that.imageUrl ]-331', 'font-size:13px; background:pink; color:#bf2c9f;', that.imageUrl)
									} else {
                    that.imageUrl = []
										that.imageUrl.push(JSON.parse(a.data).data.fullurl)
                    let imageUrl = [JSON.parse(a.data).data.fullurl]
                    that.$emit('uploadImageSuccess', imageUrl,res)
                    console.log('%c [ that.imageUrl ]-334', 'font-size:13px; background:pink; color:#bf2c9f;', that.imageUrl)
									}
									that.$refs.permissionModel.close()
									that.show = false
								},
								fail: (fail) => {
									uni.hideLoading()
									that.$refs.permissionModel.close()
								}
							})
						})
					},
					fail: (fail) => {
						console.log('取消');
						that.$refs.permissionModel.close()
					},
					cancel() {
						console.log('取消');
					}
				});
			},
			uploadAlbum() {
        console.log('%c [ uploadAlbum ]-311', 'font-size:13px; background:pink; color:#bf2c9f;', 'uploadAlbum')
				const that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					// sourceType: ['camera', 'album'], //从相册选择
					success: function(res) {
						let tempFilePaths = res.tempFilePaths
						// console.log(formData)
						let url = that.BASE_URL + 'addons/cos/index/uploads'
						console.log('%c [ url ]-663', 'font-size:13px; background:pink; color:#bf2c9f;', url)
						tempFilePaths.forEach(function(item) {
							uni.showLoading({
								title: '上传中...'
							})
							uni.uploadFile({
								url: url, //仅为示例，非真实的接口地址
								filePath: item,
								name: 'file',
								header: {
									token: uni.getStorageSync('token')
								},
								success: (a) => {
									uni.hideLoading()
									that.show = false
									console.log(a)
									console.log(JSON.parse(a.data))
									// that.uploadImgUrl = JSON.parse(a.data).data.fullurl
									// that.$emit('input', JSON.parse(a.data).data.fullurl)
									if (that.singleNum) {
                    console.log('%c [ JSON.parse(a.data).data.fullurl ]-331', 'font-size:13px; background:pink; color:#bf2c9f;', JSON.parse(a.data).data.fullurl)
                    that.imageUrl = ''
										that.imageUrl = JSON.parse(a.data).data.fullurl
                    let imageUrl = JSON.parse(a.data).data.fullurl
									  that.$emit('uploadImageSuccess', imageUrl,res)
                    console.log('%c [ that.imageUrl ]-331', 'font-size:13px; background:pink; color:#bf2c9f;', that.imageUrl)
									} else {
                    that.imageUrl = []
										that.imageUrl.push(JSON.parse(a.data).data.fullurl)
                    let imageUrl = [JSON.parse(a.data).data.fullurl]
                    that.$emit('uploadImageSuccess', imageUrl,res)
                    console.log('%c [ that.imageUrl ]-334', 'font-size:13px; background:pink; color:#bf2c9f;', that.imageUrl)
									}
									that.$refs.permissionModel.close()
                  console.log('%c [ that.imageUrl ]-336', 'font-size:13px; background:pink; color:#bf2c9f;', that.imageUrl)
								},
								fail: (fail) => {
									uni.hideLoading()
									that.$refs.permissionModel.close()

								}
							})
						})
					},
					fail: (fail) => {
						console.log('取消');
						that.$refs.permissionModel.close()
					},
					cancel() {
						console.log('取消');
					}
				});
			},
      sendVideoMessage(type) {
				let that=this
				uni.chooseVideo({
					sourceType: [type],
					// 来源相册或者拍摄
					maxDuration: 60,
					// 设置最长时间60s
					camera: 'back',
					// 后置摄像头
					success: res => {
						uni.showLoading({
							title: ''
						})
						if (res) {
							console.log(res,11111222233333)
							uni.uploadFile({
								url: that.BASE_URL+'addons/cos/index/uploads', //仅为示例，非真实的接口地址
								filePath: res.tempFilePath,
								name: 'file',
								header:{
									token:uni.getStorageSync('token')
								},
								success: (a) => {
									uni.hideLoading()
									console.log(a)
									console.log(JSON.parse(a.data))
									// that.uploadImgUrl = JSON.parse(a.data).data.fullurl
									// that.$emit('input', JSON.parse(a.data).data.fullurl)
									if (that.singleNum) {
                    console.log('%c [ JSON.parse(a.data).data.fullurl ]-331', 'font-size:13px; background:pink; color:#bf2c9f;', JSON.parse(a.data).data.fullurl)
                    that.imageUrl = ''
										that.imageUrl = JSON.parse(a.data).data.fullurl
                    let imageUrl = JSON.parse(a.data).data.fullurl
									  that.$emit('uploadVideoSuccess', imageUrl,res)
                    console.log('%c [ that.imageUrl ]-331', 'font-size:13px; background:pink; color:#bf2c9f;', that.imageUrl)
									} else {
                    that.imageUrl = []
										that.imageUrl.push(JSON.parse(a.data).data.fullurl)
                    let imageUrl = [JSON.parse(a.data).data.fullurl]
                    that.$emit('uploadVideoSuccess', imageUrl,res)
                    console.log('%c [ that.imageUrl ]-334', 'font-size:13px; background:pink; color:#bf2c9f;', that.imageUrl)
									}
									that.$refs.permissionModel.close()
									that.show = false
								},
							})
						}
					}
				});
			},
		},
	};
</script>